package com.ihr360.payroll.controller.salarytasklet;

import com.ihr360.authority.sharing.valid.AuthValid;
import com.ihr360.authority.sharing.valid.EnableAuthValidation;
import com.ihr360.authority.sharing.validator.GetOneValidator;
import com.ihr360.job.core.entity.StepExecutionLog;
import com.ihr360.job.core.model.ProgressInfo;
import com.ihr360.job.core.repository.PageData;
import com.ihr360.maven.plugin.ModuleCodeStore;
import com.ihr360.maven.plugin.annotation.FunctionDefinition;
import com.ihr360.maven.plugin.annotation.ModuleDefinition;
import com.ihr360.payroll.model.dto.salary.plan.SalaryPlan;
import com.ihr360.payroll.service.salarytask.SalaryTaskLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 薪资核算
 */
@Api(value = "薪资核算相关接口")
@RestController
@RequestMapping("/salary/tasklet/log")
@ModuleDefinition(mainModuleCode = ModuleCodeStore.Module.SALARY_CODE,
        mainModuleName = ModuleCodeStore.Module.SALARY_NAME,
        subModuleCode = ModuleCodeStore.Module.Salary.SALARY_TASK_CODE,
        subModuleName = ModuleCodeStore.Module.Salary.SALARY_TASK_NAME)
public class SalaryTaskLogController {
    @Autowired
    private SalaryTaskLogService salaryTaskLogService;

    @FunctionDefinition(functionName = "加载包含trace类型的首页日志", functionCode = ModuleCodeStore.Function.VIEW)
    @ApiOperation(value = "加载包含trace类型的首页日志")
    @GetMapping(value = "/top/allLevel", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @EnableAuthValidation
    public PageData<StepExecutionLog> topAllLevelLogs(
            @AuthValid(validator = GetOneValidator.class, entity = SalaryPlan.class) @RequestParam Long salaryPlanId,
            @RequestParam(name = "jobExecutionIds", required = false) List<Long> executionIds,
                                               @RequestParam(name = "pageSize") int pageSize,
                                               @RequestParam(name = "lastLogId") int lastLogId) {
        return salaryTaskLogService.topLogs(executionIds, pageSize, lastLogId);
    }

    @FunctionDefinition(functionName = "加载首页日志", functionCode = ModuleCodeStore.Function.VIEW)
    @ApiOperation(value = "加载首页日志")
    @GetMapping(value = "/top", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @EnableAuthValidation
    public PageData<StepExecutionLog> topLogs(
            @AuthValid(validator = GetOneValidator.class, entity = SalaryPlan.class) @RequestParam Long salaryPlanId,
            @RequestParam(name = "jobExecutionIds", required = false) List<Long> executionIds,
                                       @RequestParam(name = "pageSize") int pageSize,
                                       @RequestParam(name = "lastLogId") int lastLogId) {
        return salaryTaskLogService.topLogsNoTrace(executionIds, pageSize, lastLogId);
    }

    @FunctionDefinition(functionName = "加载首页错误类型的日志", functionCode = ModuleCodeStore.Function.VIEW)
    @ApiOperation(value = "加载首页错误类型的日志")
    @GetMapping(value = "/top/error", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @EnableAuthValidation
    public PageData<StepExecutionLog> topErrorLogs(
            @AuthValid(validator = GetOneValidator.class, entity = SalaryPlan.class) @RequestParam Long salaryPlanId,
            @RequestParam(name = "jobExecutionIds", required = false) List<Long> executionIds,
                                            @RequestParam(name = "pageSize") int pageSize,
                                            @RequestParam(name = "lastLogId") int lastLogId) {
        return salaryTaskLogService.topLogsOnlyError(executionIds, pageSize, lastLogId);
    }

    @FunctionDefinition(functionName = "加载包含trace类型的日志", functionCode = ModuleCodeStore.Function.VIEW)
    @ApiOperation(value = "加载包含trace类型的日志")
    @GetMapping(value = "/more/allLevel", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @EnableAuthValidation
    public PageData<StepExecutionLog> moreAllLevelLogs(
            @AuthValid(validator = GetOneValidator.class, entity = SalaryPlan.class) @RequestParam Long salaryPlanId,
            @RequestParam(name = "jobExecutionIds", required = false) List<Long> executionIds,
                                                @RequestParam(name = "pageSize") int pageSize,
                                                @RequestParam(name = "lastLogId") int lastLogId) {
        return salaryTaskLogService.moreLogs(executionIds, pageSize, lastLogId);
    }

    @FunctionDefinition(functionName = "加载更多日志", functionCode = ModuleCodeStore.Function.VIEW)
    @ApiOperation(value = "加载更多日志")
    @GetMapping(value = "/more", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @EnableAuthValidation
    public PageData<StepExecutionLog> moreLogs(
            @AuthValid(validator = GetOneValidator.class, entity = SalaryPlan.class) @RequestParam Long salaryPlanId,
            @RequestParam(name = "jobExecutionIds", required = false) List<Long> executionIds,
                                        @RequestParam(name = "pageSize") int pageSize,
                                        @RequestParam(name = "lastLogId") int lastLogId) {
        return salaryTaskLogService.moreLogsNoTrace(executionIds, pageSize, lastLogId);
    }

    @FunctionDefinition(functionName = "加载更多错误类型的日志", functionCode = ModuleCodeStore.Function.VIEW)
    @ApiOperation(value = "加载更多错误类型的日志")
    @GetMapping(value = "/more/error", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @EnableAuthValidation
    public PageData<StepExecutionLog> moreErrorLogs(
            @AuthValid(validator = GetOneValidator.class, entity = SalaryPlan.class) @RequestParam Long salaryPlanId,
            @RequestParam(name = "jobExecutionIds", required = false) List<Long> executionIds,
                                             @RequestParam(name = "pageSize") int pageSize,
                                             @RequestParam(name = "lastLogId") int lastLogId) {
        return salaryTaskLogService.moreLogsOnlyError(executionIds, pageSize, lastLogId);
    }

    @FunctionDefinition(functionName = "获取错误类型的日志数量", functionCode = ModuleCodeStore.Function.VIEW)
    @ApiOperation(value = "获取错误类型的日志数量")
    @GetMapping(value = "/count/error", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @EnableAuthValidation
    public Long errorLogCount(
            @AuthValid(validator = GetOneValidator.class, entity = SalaryPlan.class) @RequestParam Long salaryPlanId,
            @RequestParam(name = "jobExecutionIds", required = false) List<Long> executionIds) {
        return salaryTaskLogService.getErrorLogCount(executionIds);
    }

    @FunctionDefinition(functionName = "获取警告类型的日志数量", functionCode = ModuleCodeStore.Function.VIEW)
    @ApiOperation(value = "获取警告类型的日志数量")
    @GetMapping(value = "/count/warning", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @EnableAuthValidation
    public Long warningLogCount(
            @AuthValid(validator = GetOneValidator.class, entity = SalaryPlan.class) @RequestParam Long salaryPlanId,
            @RequestParam(name = "jobExecutionIds", required = false) List<Long> executionIds) {
        return salaryTaskLogService.getWarningLogCount(executionIds);
    }

    @FunctionDefinition(functionName = "加载薪资核算的执行进度", functionCode = ModuleCodeStore.Function.VIEW)
    @ApiOperation(value = "加载薪资核算的执行进度")
    @GetMapping(value = "/progress", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @EnableAuthValidation
    public ProgressInfo getProgressInfo(
            @AuthValid(validator = GetOneValidator.class, entity = SalaryPlan.class) @RequestParam Long salaryPlanId,
            @RequestParam(name = "jobExecutionId") Long executionId) {
        return salaryTaskLogService.getProgressInfo(executionId);
    }

}